import { defineStore } from 'pinia'
import router from '../../router/index'
import { loginUp } from '@/api/login/login'
import { ElMessage } from 'element-plus'

export default defineStore('login', {
  state: () => {
    return {
      isCollapse: false,
      username: '',
      password: '',
      role: '',
      lastTime: '',
      lastAddress: ''
    }
  },
  actions: {
    async loginUp(data: any) {
      const res = await loginUp(data)
      if (res.status == 200) {
        this.username = res.data.username
        this.password = res.data.password
        this.role = res.data.role
        this.lastTime = res.data.lastTime
        this.lastAddress = res.data.lastAddress
        localStorage.setItem('token', res.data.token)
        router.push('/home')
      } else {
        ElMessage({
          message: '账号或密码错误！',
          type: 'error'
        })
      }
      return res
    }
  },
  // 持久化存储
  persist: {
    enabled: true,
    strategies: [
      { storage: localStorage }
    ]
  }
}
)